package cn.yanglq.Rss.web.service.impl;

import cn.yanglq.Rss.web.dao.RandomCodeMapper;
import cn.yanglq.Rss.web.dao.UserMapper;
import cn.yanglq.Rss.web.model.RandomCode;
import cn.yanglq.Rss.web.model.RandomCodeExample;
import cn.yanglq.Rss.web.model.User;
import cn.yanglq.Rss.web.model.UserExample;
import cn.yanglq.Rss.web.service.RandomCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yanglq on 2018/1/19.
 */
@Service
public class RandomCodeServiceImpl implements RandomCodeService {
    @Autowired
    private RandomCodeMapper randomCodeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public RandomCode getRandomCode(String regEmail) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andEmailEqualTo(regEmail);

        List<User> users = userMapper.selectByExample(example);
        List<RandomCode> codes = null;
        if (users != null) {
            RandomCodeExample example1 = new RandomCodeExample();
            RandomCodeExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andUseridEqualTo(users.get(0).getUserid())
                    .andStatusEqualTo((short) 0);
            example1.setOrderByClause("createtime desc");
            codes = randomCodeMapper.selectByExample(example1);
        }
        return codes.size() != 0 ? codes.get(0) : null;
    }

    @Override
    public void updateCodeRecord(RandomCode log) {
        randomCodeMapper.updateByPrimaryKey(log);
    }

    @Override
    public int saveCodeRecord(RandomCode log) {
        randomCodeMapper.insert(log);
        return 0;
    }

}
